package com.ruoyi.web.controller.system;

import java.util.ArrayList;
import java.util.List;

import com.ruoyi.common.core.domain.Ztree;
import com.ruoyi.common.core.domain.entity.SysDept;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.domain.SysFilter;
import com.ruoyi.system.domain.SysRight;
import com.ruoyi.system.service.ISysFilterService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.SysType;
import com.ruoyi.system.service.ISysTypeService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import java.util.Arrays;
/**
 * 【请填写功能名称】Controller
 * 
 * @author ruoyi
 * @date 2024-09-05
 */
@Controller
@RequestMapping("/system/type")
public class SysTypeController extends BaseController
{
    private String prefix = "system/type";

    @Autowired
    private ISysTypeService sysTypeService;
    @Autowired
    private ISysFilterService sysFilterService;

    @RequiresPermissions("system:type:view")
    @GetMapping()
    public String type()
    {
        return prefix + "/type";
    }

    /**
     * 查询【请填写功能名称】列表
     */
    @RequiresPermissions("system:type:list")
    @PostMapping("/list")
    @ResponseBody
    public List<SysType> list(SysType sysType)
    {
        List<SysType> sysTypeList = sysTypeService.selectSysTypeList(sysType);
        return sysTypeList;
    }

    /**
     * 导出【请填写功能名称】列表
     */
    @RequiresPermissions("system:type:export")
    @Log(title = "【请填写功能名称】", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(SysType sysType)
    {
        List<SysType> list = sysTypeService.selectSysTypeList(sysType);
        ExcelUtil<SysType> util = new ExcelUtil<SysType>(SysType.class);
        return util.exportExcel(list, "【请填写功能名称】数据");
    }

    /**
     * 新增【请填写功能名称】
     */
    @GetMapping("/add/{parentId}")
    public String add(@PathVariable("parentId") Long parentId, ModelMap mmap)
    {
        SysType sysType = sysTypeService.selectById(parentId);
        mmap.put("dept", sysType);
        return prefix + "/add";
    }

    /**
     * 新增保存【请填写功能名称】
     */
    @RequiresPermissions("system:type:add")
    @Log(title = "【请填写功能名称】", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(SysType sysType)
    {
        return toAjax(sysTypeService.insertSysType(sysType));
    }

    /**
     * 修改【请填写功能名称】
     */
    @RequiresPermissions("system:type:edit")
    @GetMapping("/edit/{typeid}")
    public String edit(@PathVariable("typeid") Long typeid, ModelMap mmap)
    {
        SysType sysType = sysTypeService.selectById(typeid);
        List<String> itemList = new ArrayList<>();
        List<String> list = new ArrayList<>();
        itemList = Arrays.asList(sysType.getFilter().split(","));
        for (int i=0;i<itemList.size();i++)
        {
            list.add(sysFilterService.selectSysFilterById(Long.parseLong(itemList.get(i))).getName());
        }
        mmap.put("filterstr",String.join(",",list));
        mmap.put("sysType", sysType);
        return prefix + "/edit";
    }
    /**
     * 选择类型树
     *
     * @param deptId 部门ID
     * @param excludeId 排除ID
     */
    @GetMapping(value = { "/selectTypeTree/{deptId}", "/selectTypeTree/{deptId}/{excludeId}" })
    public String selectDeptTree(@PathVariable("deptId") Long deptId,
                                 @PathVariable(value = "excludeId", required = false) Long excludeId, ModelMap mmap)
    {

        mmap.put("dept", sysTypeService.selectById(deptId));
        mmap.put("excludeId", excludeId);
        return prefix + "/tree";
    }
    /**
     * 加载分类列表树（排除下级）
     */
    @GetMapping("/treeData/{excludeId}")
    @ResponseBody
    public List<Ztree> treeDataExcludeChild(@PathVariable(value = "excludeId", required = false) Long excludeId)
    {
        SysType dept = new SysType();
        dept.setExcludeId(excludeId);
        List<Ztree> ztrees = sysTypeService.selectDeptTreeExcludeChild(dept);
        return ztrees;
    }
    /**
     * 选择类型树
     *
     * @param deptId 部门ID
     * @param excludeId 排除ID
     */
    @GetMapping(value = { "/selectfilter/{filter}"})
    public String selectFilter(@PathVariable("filter") String filter, ModelMap mmap)
    {
        SysFilter sysFilter = new SysFilter();
        sysFilter.setPid(0l);
        List<SysFilter> sysRightList = sysFilterService.selectSysFilterList(sysFilter);
        String stringarr[];
        int[] intArray = {0};
        if(!filter.isEmpty()) {
            stringarr = filter.split(",");
            intArray = new int[stringarr.length];
            for (int i = 0; i < stringarr.length; i++) {
                intArray[i] = Integer.parseInt(stringarr[i]);
            }
        }
        List<String> list = new ArrayList<>();
        for(int i = 0;i<sysRightList.size();i++)
        {
            if(StringUtils.contains(intArray,sysRightList.get(i).getId().intValue())) {
                list.add(sysRightList.get(i).getName());
                sysRightList.get(i).setCheck(true);
            }
            else
                sysRightList.get(i).setCheck(false);
        }
        mmap.put("filter",sysRightList);
        return prefix + "/filter";
    }

    /**
     * 修改保存【请填写功能名称】
     */
    @RequiresPermissions("system:type:edit")
    @Log(title = "【请填写功能名称】", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(SysType sysType)
    {
        return toAjax(sysTypeService.updateSysType(sysType));
    }

    /**
     * 删除【请填写功能名称】
     */
    @RequiresPermissions("system:type:remove")
    @Log(title = "删除分类", businessType = BusinessType.DELETE)
    @GetMapping("/remove/{deptId}")
    @ResponseBody
    public AjaxResult remove(@PathVariable("deptId") Long deptId)
    {
        if (sysTypeService.selectDeptCount(deptId) > 0)
        {
            return AjaxResult.warn("存在下级分类,不允许删除");
        }
        if (sysTypeService.checkDeptExistUser(deptId))
        {
            return AjaxResult.warn("部门存在用户,不允许删除");
        }
        return toAjax(sysTypeService.deleteSysTypeByTypeid(deptId));
    }
}
